﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Report;

public partial class admin_view_crm_brokers : System.Web.UI.Page
{
    string PageCode = "GGRPTRADBRMVBR";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            ddlUser.Items.Clear();
            ddlUser.Items.Add(new ListItem("All", "0"));
            ddlUser.Items.Add(new ListItem("Broker", "100"));
            ddlUser.Items.Add(new ListItem("Admin", "200"));
            ddlUser.Items.Add(new ListItem("Cashier", "300"));
            if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
            {
                CheckPageAccess();
                ddlUser.Items.Clear();
                ddlUser.Items.Add(new ListItem("Broker", "100"));
            }


            lblError.Text = "";
            lblContent.Text = "";
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        LoadGrid();
    }

    protected void btnNewBroker_Click(object sender, EventArgs e)
    {
        Response.Redirect("new-crm-broker.aspx");
    }

    protected void grvBrokers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvBrokers.PageIndex = e.NewPageIndex;
        LoadGrid();

    }

    protected void grvBrokers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].Font.Bold = true;
            e.Row.Cells[2].ForeColor = System.Drawing.Color.Green;
            e.Row.Cells[2].BackColor = System.Drawing.Color.LightGray;
            Button btnDeactivate = (Button)e.Row.FindControl("btnDeactivate");
            Button btnEdit = (Button)e.Row.FindControl("btnEdit");
            Button btnPause = (Button)e.Row.FindControl("btnPause");
            Button btnResume = (Button)e.Row.FindControl("btnResume");
            btnDeactivate.Enabled = true;
            btnPause.Visible = true;
            btnResume.Visible = false;
            btnPause.Enabled = true;
            btnResume.Enabled = true;
            string UserRole = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserRole"));
            string ActiveStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ActiveStatus"));
            string LeadAssignStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "LeadAssignStatus"));
            if (ActiveStatus.ToUpper() == "FALSE")
            {
                e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
                btnDeactivate.Enabled = false;
                btnPause.Enabled = false;
                btnResume.Enabled = false;
                if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
                {
                    btnEdit.Enabled = false;
                }
            }
            if (LeadAssignStatus.ToUpper() == "FALSE")
            {
                btnPause.Visible = false;
                btnResume.Visible = true;
            }
            if (UserRole.ToUpper() != "BROKER")
            {
                btnPause.Enabled = false;
                btnResume.Enabled = false;
            }
            Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
            if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
            {
                if (PageAccess == 0 || PageAccess == 100)
                {
                    btnDeactivate.Enabled = false;
                    btnEdit.Enabled = false;
                    btnPause.Enabled = false;
                    btnResume.Enabled = false;
                }
            }
        }
    }

    protected void grvBrokers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("MyDeactivate"))
        {
            if (e.CommandArgument != null)
            {
                Int64 BrokerId = 0;
                Int64 AdminId = 0;

                Int64 _result = -1;
                AdminId = Convert.ToInt64(Session[General.UserId].ToString());
                BrokerId = Convert.ToInt64(e.CommandArgument);

                BrokerAccountDB _brokeraccountdb = new BrokerAccountDB();
                _result = _brokeraccountdb.Deactivate(BrokerId, AdminId);
                
                #region Log
                Int64 _Logresult = 0;
                MyDatabase _myLogDB = new MyDatabase("GGR_DB_ReportConnectionString");
                string _spLogName = "sprocReportUserActivityLogInsertUpdateSingleItem";
                SortedList _sLogPara = new SortedList();
                #region Parameter
                _sLogPara.Add("@UserId", Session[General.UserId].ToString());
                _sLogPara.Add("@AgentId", BrokerId.ToString());
                if (_result > 0)
                {
                    _sLogPara.Add("@AssignStatus", 1);
                    _sLogPara.Add("@Message", "Deactivate was successful @ " + CE.Global.General.GetUTCDateTime().ToString("dd/MMM/yyyy hh:mm:ss tt"));
                }
                else
                {
                    _sLogPara.Add("@AssignStatus", 0);
                    _sLogPara.Add("@Message", "Deactivate failed @ " + CE.Global.General.GetUTCDateTime().ToString("dd/MMM/yyyy hh:mm:ss tt"));
                }
                #endregion
                _Logresult = _myLogDB.ExecuteSP(_spLogName, _sLogPara);
                #endregion

                if (_result > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Deactivate was successful.');", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Deactivate failed.');", true);
                }
                LoadGrid();
            }
        } 
        if (e.CommandName.Equals("MyEdit"))
        {
            if (e.CommandArgument != null)
            {
                Int64 BrokerId = 0;
                BrokerId = Convert.ToInt64(e.CommandArgument);
                if (BrokerId > 0)
                {
                    Response.Redirect("new-crm-broker.aspx?BrokerId=" + BrokerId.ToString());
                }
            }
        }
        if (e.CommandName.Equals("MyPause"))
        {
            if (e.CommandArgument != null)
            {
                Int64 BrokerId = 0;
                Int64 AdminId = 0;

                Int64 _result = -1;
                AdminId = Convert.ToInt64(Session[General.UserId].ToString());
                BrokerId = Convert.ToInt64(e.CommandArgument);

                BrokerAccountDB _brokeraccountdb = new BrokerAccountDB();
                _result = _brokeraccountdb.LeadAssignPause(BrokerId, AdminId, 1);

                #region Log
                Int64 _Logresult = 0;
                MyDatabase _myLogDB = new MyDatabase("GGR_DB_ReportConnectionString");
                string _spLogName = "sprocReportUserActivityLogInsertUpdateSingleItem";
                SortedList _sLogPara = new SortedList();
                #region Parameter
                _sLogPara.Add("@UserId", Session[General.UserId].ToString());
                _sLogPara.Add("@AgentId", BrokerId.ToString());
                if (_result > 0)
                {
                    _sLogPara.Add("@AssignStatus", 1);
                    _sLogPara.Add("@Message", "Auto lead assign pause was successful @ " + CE.Global.General.GetUTCDateTime().ToString("dd/MMM/yyyy hh:mm:ss tt"));
                }
                else
                {
                    _sLogPara.Add("@AssignStatus", 0);
                    _sLogPara.Add("@Message", "Auto lead assign pause failed @ " + CE.Global.General.GetUTCDateTime().ToString("dd/MMM/yyyy hh:mm:ss tt"));
                }
                #endregion
                _Logresult = _myLogDB.ExecuteSP(_spLogName, _sLogPara);
                #endregion

                if (_result > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Auto lead assign pause was successful.');", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Auto lead assign pause failed.');", true);
                }
                LoadGrid();
            }
        }
        if (e.CommandName.Equals("MyResume"))
        {
            if (e.CommandArgument != null)
            {
                Int64 BrokerId = 0;
                Int64 AdminId = 0;

                Int64 _result = -1;
                AdminId = Convert.ToInt64(Session[General.UserId].ToString());
                BrokerId = Convert.ToInt64(e.CommandArgument);

                BrokerAccountDB _brokeraccountdb = new BrokerAccountDB();
                _result = _brokeraccountdb.LeadAssignResume(BrokerId, AdminId, 0);

                #region Log
                Int64 _Logresult = 0;
                MyDatabase _myLogDB = new MyDatabase("GGR_DB_ReportConnectionString");
                string _spLogName = "sprocReportUserActivityLogInsertUpdateSingleItem";
                SortedList _sLogPara = new SortedList();
                #region Parameter
                _sLogPara.Add("@UserId", Session[General.UserId].ToString());
                _sLogPara.Add("@AgentId", BrokerId.ToString());
                _sLogPara.Add("@AssignStatus", 0);
                if (_result > 0)
                {
                    _sLogPara.Add("@Message", "Auto lead assign resume was successful @ " + CE.Global.General.GetUTCDateTime().ToString("dd/MMM/yyyy hh:mm:ss tt"));
                }
                else
                {
                    _sLogPara.Add("@Message", "Auto lead assign resume failed @ " + CE.Global.General.GetUTCDateTime().ToString("dd/MMM/yyyy hh:mm:ss tt"));
                }
                #endregion
                _Logresult = _myLogDB.ExecuteSP(_spLogName, _sLogPara);
                #endregion

                if (_result > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Auto lead assign resume was successful.');", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Auto lead assign resume failed.');", true);
                }
                LoadGrid();
            }
        }
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        DataSet _dsSummary = new DataSet();
        DataTable _dtSummary = new DataTable();
        Int16 User = Convert.ToInt16(ddlUser.Text);
        Int16 Campaign = Convert.ToInt16(ddlCampaign.Text);
        Int32 Renewal = Convert.ToInt16(ddlRenewal.Text);
        Int16 Status = Convert.ToInt16(ddlStatus.Text);

        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        string _spName = "sprocBrokerSelectItemList";
        SortedList _sPara = new SortedList();

        _sPara.Add("@User", User);
        _sPara.Add("@Campaign", Campaign);
        _sPara.Add("@Renewal", Renewal);
        _sPara.Add("@Status", Status);

        _dsSummary = _db.GetDataSet(_spName, _sPara);
        if (_dsSummary != null && _dsSummary.Tables.Count > 0 && _dsSummary.Tables[0].Rows.Count > 0)
        {
            _dtSummary = _dsSummary.Tables[0];
            grvBrokers.DataSource = _dtSummary;
            grvBrokers.DataBind();
            EnablePrintView();
        }
        else
        {
            grvBrokers.DataSource = null;
            grvBrokers.DataBind();
            EnablePrintView();
        }

    }

    private void EnablePrintView()
    {
        if (grvBrokers.Rows.Count > 0)
        {
            lblRecordCount.Text = "(" + grvBrokers.Rows.Count.ToString() + ") record(s). Date/Time: " + CE.Global.General.GetUTCDateTime().ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
        }
        else
        {
            lblRecordCount.Text = "";
        }
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            //btnDownLoad.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            //btnDownLoad.Enabled = true;
        }
    }

}
